/**
 * jims
 */
package com.jims.master.modules.operation.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.operation.dao.OperationMasterDao;
import com.jims.master.modules.operation.dao.OperationNameAfterDao;
import com.jims.master.modules.operation.dao.OperationScheduleDao;
import com.jims.master.modules.operation.dao.OperationStaffAfterDao;
import com.jims.master.modules.operation.entity.OperationMaster;
import com.jims.master.modules.operation.entity.OperationNameAfter;
import com.jims.master.modules.operation.entity.OperationSchedule;
import com.jims.master.modules.operation.entity.OperationStaffAfter;
import com.jims.master.modules.utils.OperationStaffUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 术后登记手术Service
 * @author ZhuQ
 * @version 2017-04-13
 */
@Service
@Transactional(readOnly = true)
public class OperationMasterService extends CrudService<OperationMasterDao, OperationMaster> {
    @Autowired
    private OperationScheduleDao operationScheduleDao;
    @Autowired
    OperationStaffAfterDao operationStaffAfterDao;
    @Autowired
    OperationNameAfterDao operationNameAfterDao;


	public OperationMaster get(String id) {
		return super.get(id);
	}
	
	public List<OperationMaster> findList(OperationMaster operationMaster) {
		return super.findList(operationMaster);
	}
	
	public Page<OperationMaster> findPage(Page<OperationMaster> page, OperationMaster operationMaster) {
		return super.findPage(page, operationMaster);
	}

    /*
    * 术后登记：按照手术时间查询未登记/已登记手术列表
    * @ZhuQ
    * */
    public Page<OperationMaster> findByOperationTime(Page<OperationMaster> page, OperationMaster operationMaster) {
        OperationSchedule operationSchedule=new OperationSchedule();
        if(operationMaster.getBatch()){
            operationSchedule.setOperStatus("2");
        }else {
            operationSchedule.setOperStatus("1");
        }
        operationMaster.setOperationSchedule(operationSchedule);
        operationMaster.updateOrgId();
        operationMaster.setPage(page);
        page.setList(dao.findByOperationTime(operationMaster));
        return page;
    }

    /*
    * 术后登记：根据clinicId获取手术主表的信息
    * @ZhuQ
    * */
    public OperationMaster findById(OperationMaster operationMaster) {
        //判断手术状态
        operationMaster.updateOrgId();

        if(operationMaster.getId()!=null && !"".equals(operationMaster.getId())){  //已术后登记

            operationMaster = dao.get(operationMaster.getScheduledId());

            //获取各个位置人员
            OperationStaffAfter operationStaffAfter=new OperationStaffAfter();
            operationStaffAfter.setRemarks("术后");
            operationStaffAfter.setScheduleId(operationMaster.getScheduledId());
            OperationStaffAfter zdStaffBefore=getStaff(operationStaffAfter, "0");
            OperationStaffAfter myStaffBefore=getStaff(operationStaffAfter, "1");
            OperationStaffAfter sxStaffBefore=getStaff(operationStaffAfter, "2");
            OperationStaffAfter mzStaffBefore=getStaff(operationStaffAfter, "3");
            OperationStaffAfter yzStaffBefore=getStaff(operationStaffAfter, "4");
            OperationStaffAfter gyStaffBefore=getStaff(operationStaffAfter, "5");
            OperationStaffAfter stStaffBefore=getStaff(operationStaffAfter, "6");
            operationMaster.setZdStaffBefore(zdStaffBefore);
            operationMaster.setMyStaffBefore(myStaffBefore);
            operationMaster.setSxStaffBefore(sxStaffBefore);
            operationMaster.setMzStaffBefore(mzStaffBefore);
            operationMaster.setYzStaffBefore(yzStaffBefore);
            operationMaster.setGyStaffBefore(gyStaffBefore);
            operationMaster.setStStaffBefore(stStaffBefore);
            //查询开的手术list
            OperationNameAfter operationNameAfter=new OperationNameAfter();
            operationNameAfter.setMasterId(operationMaster.getScheduledId());
            operationMaster.setOperationNameAfterList(operationNameAfterDao.findByMasterId(operationNameAfter));

        }else { //未术后登记
            operationMaster=dao.findByScheduledId(operationMaster);

            //获取各个位置人员
            OperationStaffAfter operationStaffAfter=new OperationStaffAfter();
            operationStaffAfter.setScheduleId(operationMaster.getScheduledId());
            operationStaffAfter.setRemarks("术前");
            OperationStaffAfter zdStaffBefore=getStaff(operationStaffAfter, "0");
            OperationStaffAfter myStaffBefore=getStaff(operationStaffAfter, "1");
            OperationStaffAfter sxStaffBefore=getStaff(operationStaffAfter, "2");
            OperationStaffAfter mzStaffBefore=getStaff(operationStaffAfter, "3");
            OperationStaffAfter yzStaffBefore=getStaff(operationStaffAfter, "4");
            OperationStaffAfter gyStaffBefore=getStaff(operationStaffAfter, "5");
            OperationStaffAfter stStaffBefore=getStaff(operationStaffAfter, "6");
            operationMaster.setZdStaffBefore(zdStaffBefore);
            operationMaster.setMyStaffBefore(myStaffBefore);
            operationMaster.setSxStaffBefore(sxStaffBefore);
            operationMaster.setMzStaffBefore(mzStaffBefore);
            operationMaster.setYzStaffBefore(yzStaffBefore);
            operationMaster.setGyStaffBefore(gyStaffBefore);
            operationMaster.setStStaffBefore(stStaffBefore);
            //查询开的手术list
            OperationNameAfter operationNameAfter=new OperationNameAfter();
            operationNameAfter.setMasterId(operationMaster.getScheduledId());
            operationMaster.setOperationNameAfterList(operationNameAfterDao.findByMasterIdBefore(operationNameAfter));

            operationMaster=dao.findByScheduledId(operationMaster);
        }
        return operationMaster;
    }


    @Transactional(readOnly = false)
	public int save(OperationMaster operationMaster) {
		return super.save(operationMaster);
	}

    @Transactional(readOnly = false)
    public int saveOperationMaster(OperationMaster operationMaster) {

        int num = 1 ;

        //判断master表是修改还是新增
        if(operationMaster.getId()==null || "".equals(operationMaster.getId())){
            operationMaster.updateOrgId();
            operationMaster.preInsert();
            operationMaster.setId(operationMaster.getScheduledId());
            num = num * dao.insert(operationMaster);
        }else {
            operationMaster.updateOrgId();
            operationMaster.preUpdate();
            num = num * dao.update(operationMaster);
        }

        // 先删除再新增保存手术项目
        num = num * operationNameAfterDao.delByMaster(operationMaster.getId());
        if (operationMaster.getOperationNameAfterList() != null && operationMaster.getOperationNameAfterList().size() > 0) {
            for (OperationNameAfter operationNameAfter : operationMaster.getOperationNameAfterList()) {
                operationNameAfter.setMasterId(operationMaster.getId());
                num = num * operationNameAfterDao.insert(operationNameAfter);
            }
        }

        //先删除再保存手术人员
        //保存主刀医生
        if(StringUtils.isNotBlank(operationMaster.getZdStaffBefore().getUserId())){
            OperationStaffAfter operationStaffAfter=new OperationStaffAfter();
            operationMaster.getZdStaffBefore().setScheduleId(operationMaster.getId());
            operationStaffAfter=operationMaster.getZdStaffBefore();
            operationStaffAfter.preInsert();
            num = num * operationStaffAfterDao.insert(operationStaffAfter);
        }

        //保存麻醉医生
        if(StringUtils.isNotBlank(operationMaster.getMyStaffBefore().getUserId())){
            OperationStaffAfter operationStaffAfter=new OperationStaffAfter();
            operationMaster.getMyStaffBefore().setScheduleId(operationMaster.getId());
            operationStaffAfter=operationMaster.getMyStaffBefore();
            operationStaffAfter.preInsert();
            num = num * operationStaffAfterDao.insert(operationStaffAfter);
        }
        // 保存医生助手人员
        if(StringUtils.isNotBlank(operationMaster.getYzStaffBefore().getUserIds())){
            String userIds_z=operationMaster.getYzStaffBefore().getUserIds();
            String id_z=operationMaster.getId();
            String local_z=operationMaster.getYzStaffBefore().getOperationLocal();
            OperationStaffUtils.saveStaffAfter(id_z, local_z, userIds_z);
        }
        //保存麻醉助手人员
        if(StringUtils.isNotBlank(operationMaster.getMzStaffBefore().getUserIds())){
            String userIds_z=operationMaster.getYzStaffBefore().getUserIds();
            String id_z=operationMaster.getId();
            String local_z=operationMaster.getYzStaffBefore().getOperationLocal();
            OperationStaffUtils.saveStaffAfter(id_z, local_z, userIds_z);
        }
        //保存输血医生人员
        if(StringUtils.isNotBlank(operationMaster.getSxStaffBefore().getUserIds())){
            String userIds_z=operationMaster.getYzStaffBefore().getUserIds();
            String id_z=operationMaster.getId();
            String local_z=operationMaster.getYzStaffBefore().getOperationLocal();
            OperationStaffUtils.saveStaffAfter(id_z, local_z, userIds_z);
        }

        //供应护士
        if(StringUtils.isNotBlank(operationMaster.getGyStaffBefore().getUserIds())){
            String userIds_z=operationMaster.getYzStaffBefore().getUserIds();
            String id_z=operationMaster.getId();
            String local_z=operationMaster.getYzStaffBefore().getOperationLocal();
            OperationStaffUtils.saveStaffAfter(id_z, local_z, userIds_z);
        }

        //上台护士
        if(StringUtils.isNotBlank(operationMaster.getStStaffBefore().getUserIds())){
            String userIds_z=operationMaster.getYzStaffBefore().getUserIds();
            String id_z=operationMaster.getId();
            String local_z=operationMaster.getYzStaffBefore().getOperationLocal();
            OperationStaffUtils.saveStaffAfter(id_z, local_z, userIds_z);
        }
        //修改schedule表和apply表状态

        return num;
    }
	
	@Transactional(readOnly = false)
	public int delete(OperationMaster operationMaster) {
		return super.delete(operationMaster);
	}

    /**
     * 手术预约人员获取
     * @param operationStaffAfter
     * @param state
     * @return
     */
    @Transactional(readOnly = false)
    public OperationStaffAfter getStaff(OperationStaffAfter operationStaffAfter,String state){
        operationStaffAfter.setOperationLocal(state);
        if("术后".equals(operationStaffAfter.getRemarks())){
            operationStaffAfter=operationStaffAfterDao.findIds(operationStaffAfter);
        }else {
            operationStaffAfter=operationStaffAfterDao.findIdsBefore(operationStaffAfter);
        }

        if(StringUtils.isNotBlank(operationStaffAfter.getUserIds())){
            operationStaffAfter= OperationStaffUtils.getStaffAfterNames(operationStaffAfter);
        }
        return operationStaffAfter;
    }

    /**
     * emr接口--查询病人手术信息
     * @param visitId
     * @return
     * @author 赵宁
     */
	public List<BaseDto> findOperatorInfoEmr(String visitId){
       return dao.findOperatorEmr(visitId);
    }
}